package com.lagou.rpc.consumer.controller;

import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.pojo.User;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    private int[] ports = {8899,8999};
    private int i = 0;
    @RequestMapping(value = "/get{id}", method = RequestMethod.GET)
    public User getUserById(@RequestParam("id") String id){
        i++;
        int port;
        if(i % ports.length == 0) {
            port = ports[0];
        } else {
            port = ports[1];
        }
        System.out.println("访问的端口号" + port);
        IUserService userService = (IUserService) RpcClientProxy.createProxy(IUserService.class, port);
        User user = userService.getById(Integer.parseInt(id));
        return user;
    }


}
